
package com.xjc.market.frame.interceptor;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.xjc.market.acct.entity.UserObjectEntity;
import com.xjc.market.frame.exception.UnLoginException;
import com.xjc.market.frame.util.FrameUtil;

/**
 * 登录拦截器
 * @author  liangcz
 * @Date    2017年9月19日 上午9:23:46
 * @version 1.0
 */
public class LoginInterceptor implements HandlerInterceptor {

	private Logger log = Logger.getLogger(LoginInterceptor.class);

	public LoginInterceptor() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * 拦截器处理顺序：
	 * (preHandle-->preHandle-->preHandle) --> business Interceptor --> postHandle --> DispatcherServlet --> afterCompletion
	 * 返回true，执行下一个拦截器的preHandle,最后执行对应的拦截器方法
	 * 返回false，后续的Interceptor 和 对应的拦截器实体方法都不会再执行
	 */
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + path + "/";

		String queryString = request.getQueryString();
		queryString = queryString == null ? "" : "?" + queryString;
		String baseurl = request.getRequestURL().toString();
		String url = baseurl + queryString;// 获取项目地址
		
		UserObjectEntity userObjectEntity = FrameUtil.getUserObject(request.getSession());
		
		if(isIgnore(baseurl) == false && userObjectEntity == null){
			// String logurl = basePath + "acct/service/LoginService/goLogin.do";
			// response.sendRedirect(logurl);
			// request.getRequestDispatcher(basePath + "acct/service/LoginService/goLogin.do").forward(request, response);
			response.sendRedirect(basePath + "acct/service/LoginService/goLogin.do");
			// throw new UnLoginException();
			return false;
		}
		return true;
	}
	
	/**
	 * 在业务处理器处理请求执行完成后,生成视图之前执行的动作
	 */
	@Override
	public void postHandle(HttpServletRequest request, 
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub

	}

	/**
	 * 在DispatcherServlet完全处理完请求后被调用
	 * 
	 * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
	 */
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub

	}
	/**
	 * 是否是忽略的url
	 * @author liangcz
	 * @date   2017年9月18日 下午7:05:43
	 * @return boolean
	 */
	private static boolean isIgnore(String url){
		String urls = "*.css,*.js,*.ico,*.gif,*.jpg,*.png,*.json,*.bmp,*.pdf,*.PDF,*.jpeg,*.swf,*.flv,*.txt,*.htm,*.html,*.xml"
				+ ",goLogin.do,login.do,acct/login.jsp";
		List<String> urlList = Arrays.asList(urls.split(","));
		for (String str : urlList) {
			if(url.endsWith(str) == true){
				return true;
			}
		}
		return false;
	}
}